package com.budwk.app.sqfwV2.generator.baiyunqv;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.budwk.app.sqfw.dto.FireHiddenOfficeWordDTO;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.dto.dto.DeptDTO;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.budwk.app.util.poiUtils.SitePictureOfficeUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.*;

public class AuthenticRightPdfGenerator extends BasePdfGenerator {

    private String templateName;


    public AuthenticRightPdfGenerator(){}

    public AuthenticRightPdfGenerator(String templateName){
        this.templateName=templateName;
    }




    public String getTemplateName() {
        return this.templateName;
    }


    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return false;
    }




    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        AuthenticRightDTO authenticRightDTO= JSON.parseObject(formSummary.getParamData(), AuthenticRightDTO.class);
        String  arrivalTimeStr=DateUtil.format(authenticRightDTO.getArrivalTime(), "yyyy年MM月dd日HH时mm分");
        String createTimeStr=DateUtil.format(formSummary.getCreateDate(), DatePattern.CHINESE_DATE_PATTERN);
        String  coordinateTimeStr=DateUtil.format(authenticRightDTO.getCoordinateTime(), DatePattern.CHINESE_DATE_PATTERN);
        String deptStr="";
        String deptStrYes="";
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getCompetentDepartment())){
            List<String> detpList=new ArrayList<>();
            authenticRightDTO.getCompetentDepartment().forEach(s->{
                detpList.add(s.getName());
            });
            deptStr = String.join(",",detpList);
        }
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getCompetentDepartment())){
            List<String> detpList=new ArrayList<>();
            authenticRightDTO.getCompetentDepartment().forEach(s->{
                if(s.getDoesItBelongToYourCompany().equals("是")){
                    detpList.add(s.getName());
                }

            });
            deptStrYes = String.join(",",detpList);
        }
        paramMap.put("deptList",deptStr );
        paramMap.put("deptStrYes",deptStrYes );
        paramMap.put("arrivalTimeStr",arrivalTimeStr );
        paramMap.put("coordinateTimeStr",coordinateTimeStr );
        paramMap.put("createTimeStr",createTimeStr );
        paramMap.put("checkedUnitLeader",CommonFormOfficeUtils.addWordPicture(authenticRightDTO.getCheckedUnitLeader(), 2.0F) );
        List<DeptDTO> deptDTOS=authenticRightDTO.getCompetentDepartment();
        int index=0;
       if(CollectionUtil.isNotEmpty(deptDTOS)){
           for (DeptDTO s : deptDTOS) {
               index=index+1;
               s.setIndex(index);
               if(StrUtil.isNotBlank(s.getSignature())){
                   s.setImg(CommonFormOfficeUtils.addWordPicture(s.getSignature(), 2.0F));
               }
           }
       }
        List<Map<String,PictureRenderData>> visitPictures=new ArrayList<>();
        List<Map<String,PictureRenderData>>  huifangVisitPictures=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getHuifangVisitPictures())){
            for (String huifang : authenticRightDTO.getHuifangVisitPictures()) {
                Map<String, PictureRenderData> map=new HashMap<>();
                map.put("pictures",CommonFormOfficeUtils.addWordPicture(huifang,4.0F));
                huifangVisitPictures.add(map);
            }
        }
        if(CollectionUtil.isNotEmpty(authenticRightDTO.getVisitPictures())){
            paramMap.put("visitText", StrUtil.format("会议协调情况{}现场图片",DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN)));
            for (String visitPicture : authenticRightDTO.getVisitPictures()) {
                Map<String, PictureRenderData> map=new HashMap<>();
                map.put("pictures",CommonFormOfficeUtils.addWordPicture(visitPicture,8.0F));
                visitPictures.add(map);
            }
        }

        if(StrUtil.isNotEmpty(authenticRightDTO.getMergeNotes())){
            paramMap.put("mergeNotesText", "案件合并备注：");
        }else {
            paramMap.put("mergeNotesText", "");
        }

        paramMap.put("visitPictures", visitPictures);
        paramMap.put("huifangVisitPictures", huifangVisitPictures);
        paramMap.put("deptDTOS",deptDTOS );

    }


    @Override
    protected String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return this.templateName;
    }
    //盖章
    protected void createXWPDFTemplateAfter(XWPFTemplate template, PdfGenerateParam pdfGenerateParam) {
//        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
//        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
//        AuthenticRightDTO authenticRightDTO= JSON.parseObject(formSummary.getParamData(), AuthenticRightDTO.class);
//        if(!authenticRightDTO.getCaseType().equals(AuthenticRightEnum.B.getName())){
//            NiceXWPFDocument xwpfDocument = template.getXWPFDocument();
//            PictureUtils.relPathImageSeal(xwpfDocument, "联系人：", "/baiyunqv/baiyunqv12345.png",
//                    300, 0, 80);
//        }


    }

    protected Map<String, RenderPolicy> createCustomerRenderPolicy(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        Map<String,RenderPolicy> renderPolicyMap = Maps.newHashMap();
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        renderPolicyMap.put("deptDTOS",policy);
        return renderPolicyMap;
    }



}
